$(function() {

	$("#grid").jqGrid(
	{
		url : '../sys/menu/list',
		datatype : "json",
		colModel : [
		{
			label : '菜单编号',
			name : 'menuCode',
			width : 40,
			key : true,
			sortable : true
		},
		{
			label : '菜单名称',
			name : 'menuName',
			width : 60
		},
		{
			label : '上级菜单',
			name : 'parentCode',
			width : 60
		},
		{
			label : '菜单图标',
			name : 'iconUrl',
			width : 50,
			formatter : function(value, options, row) {
				return value == null ? '' : '<i class="'
						+ value + ' fa-lg"></i>';
			}
		}, {
			label : '菜单URL',
			name : 'linkUrl',
			width : 100
		}, {
			label : '授权标识',
			name : 'accessCode',
			width : 100
		}, {
			label : '是否启用',
			name : 'isEnable',
			width : 50
		} ],
		viewrecords : true,
		height : GetWindowHeight(140),
		rowNum : 10,
		rowList : [ 10, 30, 50 ],
		rownumbers : true,
		rownumWidth : 25,
		autowidth : true,
		multiselect : true,
		pager : "#gridPager",
		jsonReader : {
			root : "page.list",
			page : "page.currPage",
			total : "page.totalPage",
			records : "page.totalCount"
		},
		prmNames : {
			page : "page",
			rows : "limit",
			order : "order"
		},
		gridComplete : function() {
			// 隐藏grid底部滚动条
			$("#grid").closest(".ui-jqgrid-bdiv").css({
				"overflow-x" : "hidden"
			});
		}
	});
});

var setting = {
	data : {
		simpleData : {
			enable : true,
			idKey : "menuCode",
			pIdKey : "parentCode",
			rootPId : -1
		},
		key : {
			name : "menuName",
			url : "nourl"
		}
	}
};

var ztree;

var vm = new Vue({
	el : '#app',
	data : {
		showList : true,
		dialogFormVisible: false,
		title : null,
		dialogFormVisible:false,
		buttonForm:{},
		buttonList : {},
		menuButton:{
			menuCode : 0,
			menuButtonList : {}
		},
		menu : {
			parentName : null,
			parentCode : 0,
			orderColumn : 0
		}
	},
	methods : {
		getMenu : function() {
			// 加载菜单树
			$.get("../sys/menu/selectMenuTree",
				function(r) {
					ztree = $.fn.zTree.init($("#menuTree"), setting,
							r.menuList);
					var node = ztree.getNodeByParam("menuCode",
							vm.menu.parentCode);
					ztree.selectNode(node);
					vm.menu.parentName = node.menuName;
				});
		},
		add : function() {
			vm.showList = false;
			vm.title = "新增菜单";
			vm.menu = {
				parentName : null,
				parentCode : 0,
				orderColumn : 0
			};
			vm.getMenu();
		},
		update : function(event) {
			var menuCode = getSelectedRow(this);
			if (menuCode == null) {
				return;
			}
			$.get("../sys/menu/info/" + menuCode, function(r) {
				vm.showList = false;
				vm.title = "修改菜单";
				vm.menu = r.menu;
			});
			vm.getMenu();
		},
		del : function(event) {
			var btn = this;
			var menuCodes = getSelectedRows(btn);
			if (menuCodes == null) {
				return;
			}
			confirm('确定要删除选中的记录？', function() {
				$.ajax({
					type : "POST",
					url : "../sys/menu/delete",
					data : JSON.stringify(menuCodes),
					success : function(r) {
						if (r.code === 0) {
							showSuccess(btn, r.msg);
							vm.reload();
						} else {
							showError(btn, r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate : function(event) {
			var btn = this;
			var url = vm.menu.menuCode == null ? "../sys/menu/add"
					: "../sys/menu/update";
			$.ajax({
				type : "POST",
				url : url,
				data : JSON.stringify(vm.menu),
				success : function(r) {
					if (r.code === 0) {
						showSuccess(btn, "保存成功！");
						vm.reload();
					} else {
						showError(btn, r.msg);
					}
				}
			});
		},
		menuTree : function() {
			layer.open({
				type : 1,
				offset : '50px',
				// skin: 'layui-layer-molv',
				title : "选择菜单",
				area : [ '300px', '450px' ],
				shade : 0,
				shadeClose : false,
				content : jQuery("#menuLayer"),
				btn : [ '确定', '取消' ],
				btn1 : function(index) {
					var node = ztree.getSelectedNodes();
					// 选择上级菜单
					vm.menu.parentCode = node[0].menuCode;
					vm.menu.parentName = node[0].menuName;
					layer.close(index);
				}
			});
		},
		reload : function(event) {
			vm.showList = true;
			var page = $("#grid").jqGrid('getGridParam', 'page');
			$("#grid").jqGrid('setGridParam', {
				page : page
			}).trigger("reloadGrid");
		},
		//设置按钮前获取数据
		setButton : function(event){
			var menuCode = getSelectedRow(this);
			if (menuCode == null) {
				return;
			}
			$.get("../sys/menu/selectMenuButton/" + menuCode, function(r) {
				vm.buttonList = r.buttonList;
				vm.menuButton.menuButtonList = r.menuButtonList;
				vm.dialogFormVisible = true;
			});
		},
		//设置按钮
		saveButton : function(event){
			var btn = this;
			var menuCode = getSelectedRow(this);
			vm.menuButton.menuCode = menuCode;
			$.ajax({
				type : "POST",
				url : "../sys/menu/saveButton",
				data : JSON.stringify(vm.menuButton),
				success : function(r) {
					if (r.code === 0) {
						showSuccess(btn, r.msg);
						vm.dialogFormVisible = false;
					} else {
						showError(btn, r.msg);
					}
				}
			});
		}
	}
});